//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class StringData : DataSource, IOfficeXmlSerializableAs
    {
        private readonly List<StringValue> actualValues = new List<StringValue>();

        public List<StringValue> StringValues
        {
            get { return actualValues; }
        }

        public override IEnumerable<object> Values
        {
            get
            {
                foreach (StringValue o in actualValues)
                    yield return o;
            }
        }

        #region IOfficeXmlSerializableAs Members

        void IOfficeXmlSerializableAs.ReadXmlAs(string name, ReadContext context)
        {
            using (var c = context.Read(name))
            {
                c.ReadOptionalElementString("ptCount");
                while (c.ReaderLocalName == "pt")
                {
                    StringValue v = new StringValue();
                    v.ReadXmlCore(c);
                    actualValues.Add(v);
                }
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializableAs.WriteXmlAs(string name, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                using (var ptCount = c.Write("ptCount"))
                {
                    ptCount.SetInteger("val", actualValues.Count);
                }
                //\\string  = c.ReadOptionalString("ptCount");
                foreach (StringValue v in actualValues)
                    v.WriteXmlCore(c);
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

        protected override void ReadXmlCore(ReadContext context)
        {
            IOfficeXmlSerializableAs t = this;
            t.ReadXmlAs("strLit", context);
        }

        protected override void WriteXmlCore(WriteContext context)
        {
            IOfficeXmlSerializableAs t = this;
            t.WriteXmlAs("strLit", context);
        }
    }
}